package cn.ac.iscas.domain.component.writer;



import cn.ac.iscas.domain.common.ColumnReflection;
import cn.ac.iscas.utils.DataSongRegexUtils;
import com.fasterxml.jackson.annotation.JsonAutoDetect;

import com.iscas.datasong.lib.common.DataSongException;
import com.iscas.datasong.lib.common.Status;
import com.iscas.datasong.lib.util.DataSongStringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by ISCAS on 2017/9/27.
 */
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)

public class DataSongWriterParameter extends WriterParameter {
	public static final String name="datasongwriter" ;

	private String host; //ip
	private int port;  //端口
	private String hbType;//add 2020.04.07 前台使用，本体中对应的类型
	private String database; //输出库
	private String table; //输出表
	private int batchSize;//批量大小
	private int bufferSize; //缓存区大小
	private List<ColumnReflection> column;//列映射 源列到目的列

	class Key {
		public static final String HOST = "host";
		public static final String PORT = "port";
		public static final String DATABASE = "database";
		public static final String TABLE = "table";
		public static final String COLUMN = "column";
		public static final String BATCHSIZE = "batchSize";
		public static final String BUFFERSIZE = "bufferSize";
	}

	@Override
	public Map<String, Object> getSimpleParameter() throws DataSongException {
		Map<String, Object> parameter = super.getSimpleParameter();
		parameter.put(Key.HOST,		host);
		parameter.put(Key.PORT,		port);
		parameter.put(Key.DATABASE,	 database);
		parameter.put(Key.TABLE,		table);
		parameter.put(Key.COLUMN,	 column);
		parameter.put(Key.BATCHSIZE, batchSize);
		parameter.put(Key.BUFFERSIZE, bufferSize);

		return parameter;
	}


	/**
	 * 校验参数是否合法
	 *
	 * @return
	 */
	@Override
	public boolean validate() throws DataSongException {
		if((DataSongStringUtils.isEmpty(this.host)) || (!DataSongRegexUtils.checkIP(this.host))){
			throw new DataSongException(Status.PARAM_ERROR,String.format("DataSongWriter [%s] is wrong!", Key.HOST));
		}
		if(this.port <= 0 ){
			throw new DataSongException(Status.PARAM_ERROR,String.format("DataSongWriter [%s] cannot be minus!", Key.PORT));
		}
		if(DataSongStringUtils.isEmpty(this.database)){
			throw new DataSongException(Status.PARAM_ERROR,String.format("DataSongWriter [%s] cannot be empty!", Key.DATABASE));
		}
		if(DataSongStringUtils.isEmpty(this.table)){
			throw new DataSongException(Status.PARAM_ERROR,String.format("DataSongWriter [%s] cannot be empty!", Key.TABLE));
		}
		if(this.batchSize <=0 ){
			throw new DataSongException(Status.PARAM_ERROR,String.format("DataSongWriter [%s] cannot be minus!", Key.BATCHSIZE));
		}
		if(this.bufferSize <=0 ){
			throw new DataSongException(Status.PARAM_ERROR,String.format("DataSongWriter [%s] cannot be minus!", Key.BUFFERSIZE));
		}
		return true;
	}
}
